package tr.com.netgis;

import tr.com.netgis.util.Constants;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 
 * //TODO daha sonra bunu tek elden kontrol edip, herhangi bir pref değiştiğinde
 * tazele
 * 
 * @author NetGis
 * 
 */
public class SharedPrefs {
	
	public static String KEY_SEND_FREQUENCY = "sendFrequencyPref";
	public static String KEY_LOCATION_SIZE = "locationSizePref";
	public static String KEY_MIN_DISTANCE = "minDistancePref";
	public static String KEY_MIN_TIME = "minTimePref";

	int locationSize;

	private long minTime;
	private float minDistance;

	/**
	 * period
	 */
	private long sendFrequency;

	private boolean sendToServer;
	private boolean useGPS;
	private String serverUrl;

	/**
	 * //TODO geçici
	 */
	private String sessionId;

	/*
	 * String ListPreference; String editTextPreference; String
	 * ringtonePreference; String secondEditTextPreference; String customPref;
	 */

	public String getSessionId() {
		return sessionId;
	}

	/**
	 * 
	 * @return Web servis url
	 */
	public String getServerUrl(){
		return serverUrl;
	}
	
	/**
	 * Pref is gather as minute so that this value must be multiple with 1000
	 * @return
	 */
	public long getSendFrequency() {
		return sendFrequency * 60 * 1000L;
	}

	public boolean isSendToServer() {
		return sendToServer;
	}
	
	public boolean useGPS() {
		return useGPS;
	}

	private Context ctx;
	private SharedPrefs() {
		this.ctx = NGApplication.getAppContext();
		getPrefs();
	}

	private static SharedPrefs INSTANCE;
	
	public static SharedPrefs getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new SharedPrefs();
		}
		INSTANCE.getPrefs();
		return INSTANCE;
	}

	/**
	 * miliseconds
	 * 
	 * @return
	 */
	public long getMinTime() {
		return minTime * 1000;
	}

	private static final String TAG = "NetMobileSharedPrefs";

	/**
	 * meters
	 * 
	 * @return
	 */
	public float getMinDistance() {
		return minDistance;
	}

	private void getPrefs() {
		// Get the xml/preferences.xml preferences
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		sendToServer = prefs.getBoolean("sendToServer", true);
		useGPS = prefs.getBoolean("useGPSPref", false);
		
		//TODO session id kaldırılacak
		//sessionId = prefs.getString("txtSessionId", Constants._SESSION_ID);
		serverUrl = prefs.getString("txtServerUrl", Constants.SERVER_URL);
		
		String sSendFrequency = prefs.getString(KEY_SEND_FREQUENCY, Long.toString(Constants.SEND_FREQUENCY));
		try {
			sendFrequency = Long.parseLong(sSendFrequency);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			sendFrequency = Constants.SEND_FREQUENCY;
		}
		String sMinTime = prefs.getString(KEY_MIN_TIME, Long.toString(Constants.MINIMUM_TIME_BETWEEN_UPDATES));
		try {
			minTime = Long.parseLong(sMinTime);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			minTime = Constants.MINIMUM_TIME_BETWEEN_UPDATES;
		}
		String sMinDistance = prefs.getString(KEY_MIN_DISTANCE, Float.toString(Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES));
		try {
			minDistance = Float.parseFloat(sMinDistance);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			minDistance = Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES;
		}
		
		
		String sLocationSize = prefs.getString(KEY_LOCATION_SIZE, Integer.toString(Constants.LOCATION_SIZE));
		try {
			locationSize = Integer.parseInt(sLocationSize);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			locationSize = Constants.LOCATION_SIZE;
		}
		
	}

	/**
	 * 
	 * @return At most how many location will be inserted in database incase of no internet connection.

	 */
	public int getLocationSize() {
		return locationSize;
	}

	public void setLocationSize(int locationSize) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_LOCATION_SIZE, Integer.toString(locationSize));
		editor.commit();
		this.locationSize = locationSize;
	}

	public void setSendFrequency(int sendFrequency) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_SEND_FREQUENCY, Integer.toString(sendFrequency));
		editor.commit();
		this.sendFrequency = sendFrequency;
	}
	
	public void setMinDistance(int minDistance) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_MIN_DISTANCE, Integer.toString(minDistance));
		editor.commit();
		this.minDistance = minDistance;
	}

	public void setMinTime(int minTime) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_MIN_TIME, Integer.toString(minTime));
		editor.commit();
		this.minTime = minTime;
	}
	
}
